﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using pTools.cMethods;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools.ChildWin
{
    public partial class BaoGong : Form
    {
        DBHelper db;
        string vplCode="",vgxCode="";
        HttpAgreement http;
        public BaoGong(string plcode,string gxCode)
        {
            InitializeComponent();
            vplCode = plcode;
            vgxCode = gxCode;
            db = new DBHelper();
            http = new HttpAgreement();
        }

        private void BaoGong_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = "生产报工";
                loadBgType();
                selBaoGongtype();
           
                dDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                tbNum.Text = "1";
                loadBaoGong();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// 报工类型
        /// </summary>
        public void loadBgType() 
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select cCode,cName from fun_CommonData_Info where cType='scgxlx' and orderNum='" + vgxCode + "'");
            sb.Append("and cCode !='' and isdel=0");
            var table = db.getData(sb.ToString(), "");
            cbBgType.DataSource = table;
            cbBgType.ValueMember = "cCode";
            cbBgType.DisplayMember = "cName";
        }
        Dictionary<string, string> type = new Dictionary<string, string>();
        public void selBaoGongtype() 
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select cCode,cName from fun_CommonData_Info where cType='bglx'");
            var table = db.getData(sb.ToString(), "");

            for (int i = 0; i < table.Rows.Count; i++)
            {
                cbBgType.Items.Add(table.Rows[i]["cName"].ToString());
                type.Add(table.Rows[i]["cName"].ToString(), table.Rows[i]["cCode"].ToString());
            }
        }
        private void tbUsers_Click(object sender, EventArgs e)
        {
            CommonMethods.selectUserCodes = "";
            CommonMethods.selectUserNames = "";
            if (string.IsNullOrWhiteSpace(vgxCode))
            {
                MessageBox.Show("请选择计划");
                return;
            }
            selectPosition win = new selectPosition(vgxCode);
            win.StartPosition = FormStartPosition.CenterScreen;
            win.WindowState = FormWindowState.Normal;
            if (win.ShowDialog() == DialogResult.OK)
            {

            }
            if (CommonMethods.userPositionTitle != null)
            {
                tbUsers.Text = CommonMethods.userPositionTitle;
            }
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                VisitService();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
         
        }
        /// <summary>
        /// 接口访问
        /// </summary>
        private void VisitService()
        {
            Dictionary<string, string> barDic = new Dictionary<string, string>();
            barDic["wType"] =cbBgType.SelectedValue.ToString();
            barDic["happenDate"] =dDate.Text;
            barDic["num"] =tbNum.Text;
            barDic["plCode"] = vplCode;
            barDic["gxCode"] = vgxCode;
            barDic["createuser"] = CommonMethods.userCode;
            barDic["userCodes"] = CommonMethods.userPositionValue; ;//生产人员编码
            barDic["positionKey"] = CommonMethods.positionKey;
            var msg = http.PostJson("ProductWage", JsonConvert.SerializeObject(barDic));
            if (!string.IsNullOrWhiteSpace(msg))
            {
                var json = JsonConvert.DeserializeObject<JObject>(msg);
                if (bool.Parse(json["Result"].ToString()))
                {
                    if (string.IsNullOrWhiteSpace(json["Data"].ToString()))
                    {
                        MessageBox.Show("报工数据保存成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        loadBaoGong();
                        return;
                    }
                }
                else
                {
                    MessageBox.Show(json["Message"].ToString(), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show("接口反馈异常", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btnCloase_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }
        private void loadBaoGong()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select c.cName bgTypeName ,pc.cName positionName,u.userName,ew.num,ew.uPrice,ew.total,");
            sb.Append("ew.happenDate,ew.zhanbi,ew.memo,ew.plCode from pw_employeeWage_info ew ");
            sb.Append("inner join fun_CommonData_Info c on c.cCode=ew.bgType ");
            sb.Append("inner join fun_CommonData_Info pc on ew.position=pc.cCode  ");
            sb.Append("inner join sys_users_info u on u.userCode=ew.userCode ");
            sb.Append("where  ew.barCode ='-' and u.baseCode='001' and ew.isdel=0 and ");
            sb.Append("ew.plCode='" + vplCode + "' and ew.happenDate='" + dDate.Text + "' and ew.wType like '" + vgxCode + "-%'");
            //sb.Append("ew.plCode='P2022020165' and ew.happenDate='2022-02-24' and ew.wType like 'ys-%'");
            sb.Append(" and u.baseCode='"+ CommonMethods.baseCode +"'");
            this.BaoGonggrid.DataSource = db.getData(sb.ToString(), "");
            this.BaoGonggrid.AutoGenerateColumns = false;

        }


        private void btnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                loadBaoGong();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (MessageBox.Show("确定要删除绩效数据?", "系统提醒", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    List<string> sqlList = new List<string>();
                    MessageBox.Show(bgKey);
                    StringBuilder sb = new StringBuilder();
         
                    sb.Append("update pw_employeeWage_info set isdel=1,delDate='" + DateTime.Now.ToString() + "',");
                    sb.Append("deluser='" + CommonMethods.userCode + "' where memo='" + bgKey + "'");
                    sqlList.Add(sb.ToString());
                    var jg = db.TransUpdate(sqlList);
                    if (jg > 0)
                    {
                        MessageBox.Show("数据删除成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("数据删除失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        string bgKey = "";
        private void BaoGonggrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                bgKey = BaoGonggrid.CurrentRow.Cells["memo"].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
